package com.haoshuang.zhouzhoubang.ui.activity

import android.os.Bundle
import com.blankj.utilcode.util.RegexUtils
import com.haoshuang.zhouzhoubang.databinding.ActivityChangePhoneNumberBinding
import com.haoshuang.zhouzhoubang.ui.vm.ChangePhoneNumberViewModel
import com.lnkj.jialubao.utils.AccountUtils
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.CountDownWorker
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger


/**
 *  修改手机号界面
 */
class ChangePhoneNumberActivity :
    BaseVMActivity<ChangePhoneNumberViewModel, ActivityChangePhoneNumberBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        binding.appBar.ivBack.clickWithTrigger { finish() }
        binding.appBar.tvTitle.text = "修改手机号"
    }

    override fun initData() {
        binding.tvPhone.text = AccountUtils.user?.phone
        binding.tvCode.clickWithTrigger {
            var phone = binding.tvPhone.text.toString()
            if (phone.isEmpty()) {
                toast("请输入手机号")
                return@clickWithTrigger
            }

            if (!RegexUtils.isMobileSimple(phone)) {
                toast("请输入有效手机号")
                return@clickWithTrigger
            }
            binding.tvCode.isEnabled = false
            vm.sendSms("mobile" to phone, "type" to "3", "sms_type" to 2)
        }
        binding.tvCode2.clickWithTrigger {
            var phone = binding.tvPhone2.text.toString()
            if (phone.isEmpty()) {
                toast("请输入手机号")
                return@clickWithTrigger
            }

            if (!RegexUtils.isMobileSimple(phone)) {
                toast("请输入有效手机号")
                return@clickWithTrigger
            }
            binding.tvCode2.isEnabled = false
            vm.sendSms2("mobile" to phone, "type" to "5", "sms_type" to 2)
        }
        binding.tvCommit.clickWithTrigger {
            var phone = binding.tvPhone.text.toString()
            var phone2 = binding.tvPhone2.text.toString()
            var codeOld = binding.codeOld.text.toString()
            var etCode = binding.etCode.text.toString()
            if (phone2.isEmpty()) {
                toast("请输入手机号")
                return@clickWithTrigger
            }
            if (codeOld.isEmpty()) {
                toast("请输入旧手机号验证码")
                return@clickWithTrigger
            }
            if (etCode.isEmpty()) {
                toast("请输入新手机号验证码")
                return@clickWithTrigger
            }
            vm.getData2("type" to 2, "phone" to phone2, "code_old" to codeOld, "code" to etCode)
        }
    }

    override fun startObserve() {
        vm.sendSmsData.observeState(this) {
            onLoading {
                showLoading()
            }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                binding.tvCode2.isEnabled = true
            }
            onSuccess {
                dismissLoading()
                toast("获取成功")
                CountDownWorker(this@ChangePhoneNumberActivity, onChange = {
                    binding.tvCode.text = "${it}S"
                }) {
                    binding.tvCode.text = "重新获取"
                    binding.tvCode.isEnabled = true
                }.start()
            }
        }
        vm.sendSmsData2.observeState(this) {
            onLoading {
                showLoading()
            }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                binding.tvCode2.isEnabled = true
            }
            onSuccess {
                dismissLoading()
                toast("获取成功")
                CountDownWorker(this@ChangePhoneNumberActivity, onChange = {
                    binding.tvCode2.text = "${it}S"
                }) {
                    binding.tvCode2.text = "重新获取"
                    binding.tvCode2.isEnabled = true
                }.start()
            }
        }
        vm.getData2.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                AccountUtils.user?.phone = binding.tvPhone2.text.toString()
                setResult(200)
                finish()
            }
        }
    }

}